;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.TimeAnalysis", [])
        .controller("TimeAnalysisCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.datePickerFlag = [false, false];

            $scope.condition = {};
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 2570,
                tableCol: [{
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件分类",
                        dataField: "materialsType",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "仓库",
                        dataField: "warehouseCode",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "库区",
                        dataField: "areaCode",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "储位",
                        dataField: "locationCode",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "单位",
                        dataField: "unit",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "当前库存",
                        dataField: "inventory",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "库存金额",
                        dataField: "inventoryAmount",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },

                    {
                        type: "",
                        name: "一个月内库存",
                        dataField: "oneMonth",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "库存金额",
                        dataField: "oneMonthAmount",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "一月到两月库存",
                        dataField: "twoMonth",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "库存金额",
                        dataField: "twoMonthAmount",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "两月到三月库存",
                        dataField: "threeMonth",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "库存金额",
                        dataField: "threeMonthAmount",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "三月到半年库存",
                        dataField: "halfYear",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "库存金额",
                        dataField: "halfYearAmount",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "半年到一年库存",
                        dataField: "oneYear",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "库存金额",
                        dataField: "oneYearAmount",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "一年到两年库存",
                        dataField: "twoYear",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "库存金额",
                        dataField: "twoYearAmount",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "两年到三年库存",
                        dataField: "threeYear",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "库存金额",
                        dataField: "threeYearAmount",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "三年以上库存",
                        dataField: "manyYear",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, {
                        type: "",
                        name: "库存金额",
                        dataField: "manyYearAmount",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "积压天数",
                        dataField: "days",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "产品组",
                        dataField: "productGroup",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "零件用途",
                        dataField: "materialUse",
                        width: 100,
                        isShow: true
                    },                    
                ]
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/report/exportInventoryOverstock")
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            var btnPermissionCode = {
                export: window.ActionCode.timeAnalysisExport
            };

            $scope.btnPermission = {
                export: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                var service = "device";
                var reqUrl = "/report/inventoryOverstock";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            (function () {
                //getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);
